#!/usr/bin/env python
#mainwindow.py

import os
import sys

import pygtk
pygtk.require("2.0")
import gtk
#import gobject

class MainWindow:
    _design_file = "mainwindow.glade"
    _window = None
    _is_destroyed = False
    _on_destroy_handler = None
    _on_execute_handler = None
    
    def __init__(self, timelist):
        scriptdir = os.path.dirname(__file__)
        
        #gladefile = os.path.join(program.glade_path, "mainwindow.glade")
        builder = gtk.Builder()
        builder.add_from_file(os.path.join(scriptdir, self._design_file))
        
        window = builder.get_object("MainWindow")
        window.set_title("G-Startup")        
        window.connect("delete-event", self._delete)
        window.connect("destroy", self._destroy)
        
        vbox = gtk.VBox()
        window.add(vbox)
        
        for tdef in timelist:
            print "Adding " + str(tdef)
            hbox = gtk.HBox()
            hbox.set_name("hbox_" + tdef.name())
            vbox.pack_start(hbox, expand = False, fill = False)
            lbl = gtk.Label(tdef.name())            
            lbl.set_name("lblName_" + tdef.name())
            lbl.set_alignment(0.0, 0.5)
            lbl.set_size_request(100, 0)
            lbl.set_padding(8, 0)
            lbl.show()            
            hbox.pack_start(lbl)
            lbl = gtk.Label(str(tdef.time_left()))
            lbl.set_name("lblTime_" + tdef.name())
            lbl.set_size_request(50, -1)
            lbl.set_padding(8, 0)
            lbl.show()            
            hbox.pack_start(lbl)
            btn = gtk.Button("Now!")
            btn.connect("clicked", self.btnnow_click, tdef)
            btn.set_name("btnNow_" + tdef.name())
            #btn.set_size_request(50, -1)
            hbox.pack_start(btn)        
            btn.show()
            hbox.show()
        
        hbox = gtk.HBox()
        vbox.pack_start(hbox, expand=False, fill=False)
        btn = gtk.Button("Stop!")
        btn.connect("clicked", self.btnstop_click)
        hbox.pack_start(btn)
        btn.show()
        hbox.show()
        
        vbox.show()
               
        self._window = window        
    
    def show(self):
        self._window.show()
    
    def show(self, left, top):
        self._window.move(left, top)
        self._window.show()
        
    def hide(self):
        self._window.hide()
        
    def update_timer(self, tdef):
        lbl = self.getwidget(self._window, "lblTime_" + tdef.name())
        if lbl != None:
            lbl.set_text(str(tdef.time_left()))
    
    def set_executed(self, name):
        btn = self.getwidget(self._window, "btnNow_" + name)
        if btn != None:
            btn.set_sensitive(False)
    
    def set_on_execute_handler(self, func):
        self._on_execute_handler = func
    
    def set_on_destroy_handler(self, func):
        self._on_destroy_handler = func
    
    def btnstop_click(self, widget):
        self._window.destroy()
    
    def btnnow_click(self, widget, tdef):
        self._on_execute_handler(tdef)
        widget.set_sensitive(False)

    def _delete(self, widget, data=None):
        self.hide()
        return True
    
    def destroy(self):
        self._window.destroy()
        
    def _destroy(self, widget, data=None):
        self._is_destroyed = True
        if self._on_destroy_handler != None:
            self._on_destroy_handler()
    
    def is_destroyed(self):
        return self._is_destroyed
    
    def getwidget(self, parent, name):
        for child in parent.get_children():
            if child.get_name() == name:
                return child
            else:
                if isinstance(child, gtk.Container):
                    found = self.getwidget(child, name)
                    if found != None:
                        return found